package com.work.rcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.rcj.common.exception.BusinessException;
import com.work.rcj.common.model.Response;
import com.work.rcj.entity.User;
import com.work.rcj.mapper.UserMapper;
import com.work.rcj.pojo.dto.UserDto;
import com.work.rcj.pojo.vo.VisitVo;
import com.work.rcj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rcj
 * @since 2022-02-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Response<User> login(User req) {
        if(StringUtils.isEmpty(req.getPwd())){
            throw new BusinessException("500001","密码不能未空");
        }
        User user = userMapper.selectUser(req.getUserName());
        if(!req.getPwd().equals(user.getPwd())){
            throw new BusinessException("500002","密码不正确");
        }
        return Response.success(user);
    }

    @Override
    public Response updateUserInfo(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name",user.getUserName());
        userMapper.update(user,updateWrapper);
        return Response.success();
    }

    @Override
    public IPage<User> selectList(UserDto dto) {
        IPage<User> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        List<User> list = userMapper.userList(page,dto);
        page.setRecords(list);
        return page;
    }
}
